package com.wuli.food.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wuli.food.common.apiModel.R;
import com.wuli.food.dto.FoodDto;
import com.wuli.food.entity.Food;
import com.wuli.food.service.FoodService;
import com.wuli.food.vo.FoodAddVo;
import com.wuli.food.vo.FoodSearchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiongqiaoqiao
 * @since 2023-05-18
 */
@Api(tags = "菜品管理接口")
@RestController
@RequestMapping("/foodInfo")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @PostMapping("/search")
    @ApiOperation("分页条件查询")
    public R<?> search(@RequestBody FoodSearchVo searchVo) {
        IPage<FoodDto> list = foodService.search(searchVo);
        return new R<>().Success(list);
    }

    @ApiOperation("新增和修改")
    @PostMapping("/add")
    public R add(@RequestBody FoodAddVo addVo){
        String id = foodService.add(addVo);
        return new R().Success(id);
    }

    @ApiOperation("查看详情")
    @GetMapping("/getById")
    public R getDetail(@ApiParam("菜品Id") @NotBlank(message = "id不能为空") String id){
        FoodDto dot = foodService.getDetail(id);
        return new R<>().Success(dot);
    }

    @ApiOperation("删除")
    @GetMapping("/delete")
    public R delete(@ApiParam("菜品Id") @NotBlank(message = "id不能为空") String id){
        boolean b = foodService.removeById(id);
        if(b) {
            return new R<>().Success("删除成功！");
        }else {
            return new R<>().Error("删除失败！");
        }
    }

    @ApiOperation("菜品上架/下架")
    @GetMapping("/changeState")
    public R changeState(@ApiParam("菜品Id") @NotBlank(message = "id不能为空") String id,
                         @ApiParam("1上架, 0下架") @NotBlank(message = "状态不能为空") String state){
        Food byId = foodService.getById(id);
        byId.setState(state);
        foodService.updateById(byId);
        return new R<>().Success("修改菜品状态成功！");
    }

}

